package com.example.service.impl;

import com.example.dao.ShujuDao;
import com.example.entity.Shuju;
import com.example.service.ShujuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 食材库存服务实现类
 */
@Service
public class ShujuServiceImpl implements ShujuService {

    @Resource
    private ShujuDao shujuDao;

    @Override
    public PageInfo<Shuju> getShujuList(Integer pageNum, Integer pageSize, String key) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("key", key);
        
        PageHelper.startPage(pageNum, pageSize);
        List<Shuju> list = shujuDao.selectAll(map);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(Shuju shuju) {
        if (shuju.getId() == null) {
            shujuDao.add(shuju);
        } else {
            shujuDao.update(shuju);
        }
    }

    @Override
    public void deleteShuju(int id) {
        shujuDao.delete(id);
    }

    @Override
    public void batchDeleteShuju(String vals) {
        String[] val = vals.split(",");
        for (String id : val) {
            shujuDao.delete(Integer.parseInt(id));
        }
    }
} 